package istar.impl;

import java.util.HashMap;
import java.util.Iterator;

import istar.IActor;
import istar.IElement;
import istar.ITask;

public class Belief extends AbstractElement {

	public static int POSITIVE = 1;
	public static int STRONGLY_POSITIVE = +2;
	public static int NEUTRAL = 0;
	public static int NEGATIVE = -1;
	public static int STRONGLY_NEGATIVE = -2;
	public boolean available;
	
	private IElement element;
	private Double probability;
	//private Double value;
	//private int assessment = NEUTRAL;
	private HashMap<ITask, Double> assessments = new HashMap<ITask, Double>();
	private EvaluationLink evaluationLink;
	
	
	public Belief(String name) {
		super(name);
	}
	
	
	public Belief(String name, IElement topic) {
		super(name);
		this.element = topic;
	}

	public Belief(String name, IElement topic, IActor owner){
		super(name, owner);
		this.element = topic;
	}
	
	public Belief(String name, IActor owner){
		super(name, owner);
	}
	/*
	public Belief(String name, IElement topic, double value, IActor owner){
		super(name, owner);
		this.element = topic;
		this.value = value;
	}
	*/
	
	public Double getProbability() {
		return probability;
	}
	public void setProbability(Double _probability) throws ExceptionInInitializerError {
		if (_probability > 1 || _probability < 0)
			throw new ExceptionInInitializerError("Invalid probabality!");
		else 
			this.probability = _probability;
	}

	public IElement getElement() {
		return this.element;
	}

	public void setElement(IElement _elmnt) {
		this.element = _elmnt;
	}

	
	
	public EvaluationLink getEvaluationLink() {
		return evaluationLink;
	}


	public void setEvaluationLink(EvaluationLink evaluationLink) {
		this.evaluationLink = evaluationLink;
	}


	public HashMap<ITask, Double> getAssessments() {
		return assessments;
	}
	
	public Double getAssessment(ITask task) {
		Double assess = new Double(NEUTRAL);
		Iterator<ITask> itMeans = assessments.keySet().iterator();
		while (itMeans.hasNext()){
			ITask mean = itMeans.next();
			if (mean.equals(task))
				assess = (Double)assessments.get(mean);
		}
		
		return assess;
	}

	/*
	public void setAssessment(int assessment) {
		this.assessment = assessment;
	}

	public int getOpositeAssessment() {
		return -assessment;
	}
	 */

	public boolean isAvailable() {
		return available;
	}


	public void setAvailable(boolean available) {
		this.available = available;
	}


	@Override
	public boolean achieved() {
		// TODO Auto-generated method stub
		return this.available;
	}


	@Override
	protected Object clone() throws CloneNotSupportedException {
		
		Belief newClone = (Belief) super.clone();
		newClone.element = this.element;
		newClone.evaluationLink = (EvaluationLink)this.evaluationLink.clone();
		newClone.probability = this.probability;
		newClone.assessments = new HashMap<ITask, Double>(this.assessments);
		return newClone;
	}


	@Override
	public void clear() {
		// TODO Auto-generated method stub
		super.clear();
		this.assessments.clear();
	}

	
	
}
